package com.saris.primego.web.controller.admin;

import com.saris.primego.common.result.Result;
import com.saris.primego.pojo.vo.UploadFileVO;
import com.saris.primego.service.CommonService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

@RestController("adminCommonController")
@RequestMapping("/admin/common")
@Tag(name = "通用接口")
@Slf4j
public class CommonController {

    private final CommonService commonService;

    public CommonController(CommonService commonService) {
        this.commonService = commonService;
    }

    @PostMapping("/upload")
    @Operation(summary = "文件本地上传", method = "POST")
    public Result<String> upload(MultipartFile file) {

        log.info("上传的文件为：{}", file.getOriginalFilename());

        UploadFileVO uploadFileVO = commonService.uploadToLocal(file);

        log.info("文件上传成功，保存路径：{}，访问URL：{}", uploadFileVO.getTargetLocation(), uploadFileVO.getFileDownloadUri());
        return Result.success(uploadFileVO.getFileDownloadUri());
    }
}